package com.draemon.haixiawenti.mvc;

import com.draemon.haixiawenti.constant.InitConstantService;
import com.draemon.haixiawenti.repository.GonggaoRepository;
import com.draemon.haixiawenti.repository.HqbnewsRepository;
import com.draemon.haixiawenti.repository.NewsRepository;
import com.draemon.haixiawenti.util.DateUtil;
import com.yangzcp.userpojo.Gonggao;
import com.yangzcp.userpojo.Hqbnews;
import com.yangzcp.userpojo.News;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;

@Controller
@ApiIgnore
public class ViewController {

    @Autowired
    private NewsRepository newsRepository;

    @Autowired
    private HqbnewsRepository hqbnewsRepository;

    @Autowired
    private GonggaoRepository gonggaoRepository;

    private final static String HQB_SAI_IMG_VIEW = "hqb_news_content";
    private final static String SAI_IMG_VIEW = "news_content";
    private final static String GONGAO_VIEW = "gonggao_content";

    @RequestMapping(value = "/newscontent/{id}")
    ModelAndView newscontent(@PathVariable Integer id) {
        ModelAndView view = new ModelAndView();
        view.setViewName(SAI_IMG_VIEW);
        News one = newsRepository.findOne(id);
        view.getModel().put("data", one);
        view.getModel().put("data_date", DateUtil.format(new Date(one.getPublish_time()), "yyyy-MM-dd HH:mm:ss"));
        view.getModel().put("ADMIN_SERVICE_HOST", InitConstantService.ADMIN_SERVICE_HOST);
        return view;
    }

    @RequestMapping(value = "/hqbnewscontent/{id}")
    ModelAndView hqbnewscontent(@PathVariable Integer id) {
        ModelAndView view = new ModelAndView();
        view.setViewName(HQB_SAI_IMG_VIEW);
        Hqbnews one = hqbnewsRepository.findOne(id);
        view.getModel().put("data", one);
        view.getModel().put("data_date", DateUtil.format(new Date(one.getPublish_time()), "yyyy-MM-dd HH:mm:ss"));
        view.getModel().put("ADMIN_SERVICE_HOST", InitConstantService.ADMIN_SERVICE_HOST);
        return view;
    }

    @RequestMapping(value = "/gonggaocontent/{id}")
    ModelAndView gonggaocontent(@PathVariable Integer id) {
        ModelAndView view = new ModelAndView();
        view.setViewName(GONGAO_VIEW);
        Gonggao one = gonggaoRepository.findOne(id);
        view.getModel().put("data", one);
        view.getModel().put("data_date", DateUtil.format(new Date(one.getPublish_time()), "yyyy-MM-dd HH:mm:ss"));
        view.getModel().put("ADMIN_SERVICE_HOST", InitConstantService.ADMIN_SERVICE_HOST);
        return view;
    }
}
